import { reqCartPost, reqGetDetailList } from "@/api"
import { getUUid } from '@/utils/uuid_token'
const state = {
    detailList: {},
    // 创建uuid，通过生成的唯一值uuid来辨别游客的身份
    uuid_token: getUUid()
}
const mutations = {
    GETDETAIL(state, detailList) {
        state.detailList = detailList
    }
}
const actions = {
        async getDetail({ commit }, skuId) {
            let res = await reqGetDetailList(skuId)
            if (res.code === 200) {
                commit('GETDETAIL', res.data)
                console.log(res.data);
            }
        },
        async postCart({ commit }, { skuId, skuNum }) {
            let res = await reqCartPost(skuId, skuNum)
            if (res.code === 200) {
                return "ok"
            } else {
                return Promise.reject(new Error('faile'))
            }
        },

    }
    // 简化数据
const getters = {
    categoryView(state) {
        return state.detailList.categoryView || {}
    },
    skuInfo(state) {
        return state.detailList.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.detailList.spuSaleAttrList || []
    }
}
export default {
    state,
    mutations,
    actions,
    getters,
}